/**
 * @author liubaohui
 * @description 产品履历树搜索
 */
import treeUtil from '../treeUtil'

let resumeTreeSearch = {
  methods: {
    // 点击搜索
    async handleSearch() {
      if (this.keyword === '') return
      // 搜索类型为周转码
      if (this.searchType === 1) {
        // 先根据周转码查询序列号列表
        // this.serialNumbers = ['026009001', '026009002']
        // this.serialNumbers = ['026009001']
        // this.serialNumbers = []
        this.serialNumbers = await this.getSerialNumbers()
        switch (this.serialNumbers.length) {
          // 没有序列号时直接搜索节点
          case 0:
            this.searchAndLocatedNode()
            break
          // 有一个序列号时，默认以此序列号搜索节点
          case 1:
            this.serialNumber = this.serialNumbers[0]
            this.searchAndLocatedNode()
            break
          default:
            break
        }
        return
      }
      // 搜索类型为序列号
      if (this.searchType === 2) {
        return this.searchAndLocatedNode()
      }
    },
    // 序列号下拉输入框选中时按周转码+序列号搜索
    handleSerialNumberSelect() {
      this.searchAndLocatedNode()
    },
    // 搜索并定位到结果节点
    async searchAndLocatedNode() {
      // 先根据周转码号+序列号查找任务号
      // let taskNumberList = ['508N-2019-03-005']
      let taskNumberList = await this.getTaskNumbers()
      let taskNumber = taskNumberList[this.currentTaskNumberIndex]
      if (taskNumberList.length === 0) {
        return this.$message.error({
          message: '未搜索到结果',
          duration: 1500,
          center: true
        })
      }
      // 根据任务号获取工单树，插入到对应的任务号节点下
      this.matchedTaskNode = this.$refs.elTree.store.nodesMap[taskNumber]
      // taskNode.data.children = mockMaterialTree
      this.matchedTaskNode.data.children = await this.getMaterialTree(taskNumber)
      if (!Array.isArray(this.matchedTaskNode.data.children)) {
        return this.$message.error({
          message: '未搜索到结果',
          duration: 1500,
          center: true
        })
      }
      this.locatedNode()
    },
    // 找到匹配结果，将任务号及其上级节点展开
    locatedNode() {
      this.$nextTick(() => {
        this.matchedNodes = this.getMatchedNode()
        if (this.currentMatchedNodeIndex > this.matchedNodes.length - 1) {
          this.currentMatchedNodeIndex = 0
        }
        let matchedNode = this.matchedNodes[this.currentMatchedNodeIndex]
        this.targetId = matchedNode.id
        if (this.targetId === null) {
          return this.$message.error({
            message: '未搜索到结果',
            duration: 1500,
            center: true
          })
        }
        this.matchedTaskNode.parent.expanded = true
        this.matchedTaskNode.expanded = true
        // 设置结果高亮
        this.$refs.elTree.setCurrentNode(matchedNode)
        // 展开结果所在任务号所有节点
        treeUtil.expandChildNodes(this.matchedTaskNode)
        // 滚动到结果元素位置
        this.scrollToMatchedView()
        this.$emit('selectNode', matchedNode.data.number)
      })
    },
    // 得到匹配节点
    getMatchedNode() {
      return Object.values(this.$refs.elTree.store.nodesMap).filter(n => {
        if (this.searchType === 1) {
          return n.data.number && n.data.number.toUpperCase() === this.keyword.toUpperCase() && !n.isLeaf
        }
        if (this.searchType === 2) {
          return n.data.materialSerialNumber && n.data.materialSerialNumber.toUpperCase() === this.keyword.toUpperCase() && !n.isLeaf
        }
      })
    },
    // 展开任务号定位到结果元素位置
    scrollToMatchedView() {
      this.$refs.scrollbar.$refs.wrap.scrollTop = 0
      // 展开任务号后定位到元素位置
      let timer = setInterval(() => {
        let offsetTop = this.$el.querySelector('.tree_search_result_node').offsetTop
        let clientHeight = this.$refs.elTree.$el.clientHeight
        if (this.treeClientHeight === clientHeight) {
          this.$nextTick(() => {
            this.$refs.scrollbar.$refs.wrap.scrollTop = offsetTop - 4
          })
          return clearInterval(timer)
        }
        this.treeClientHeight = clientHeight
      }, 50)
    },
  }
}

export default resumeTreeSearch
